<?php

/**
 * 对信官方义函数
 */

function d_guanlizuming($quanid,$uid) {

    
	$guanliuids =\Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $quanid)->where('uid', $uid)->whereIn('zhuangtai', [1,3])->get()->getRowArray();
	if ($guanliuids) {
		$guanliuid = $guanliuids;
	} else {
		$guanliuid = '';
	}
	
	if($guanliuid){
		$guanliming =\Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('id', $guanliuid['guanlizu'])->where('cid', $quanid)->get()->getRowArray();
		$v = $guanliming['title'];
	} else {
		$v = '';
	}
	
    return $v;
	
}
/**
 * 对信官方义函数
 * $quanid 圈子id
 * $uid 用户id
 * $mingcheng 发布帖子的名称: '名称'。例如：'jiugetu'
 * $leixing 类型，1为管理员。3为管理员或者用户
 */
function d_fubu_quanxian($quanid,$uid,$mingcheng,$leixing) {
	
	$rt = \Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $quanid)->where('uid', $uid)->get()->getRowArray();
	$group = \Phpcmf\Service::M()->db->table('1_quan_form_group')->where('quanid', $quanid)->where('uid', $uid)->get()->getRowArray();
	if($group){
		$rt=$group;
	} else {
		$rt=$rt;
	}
	if($leixing==1){
		$leixing=1;
		$guanliuid =\Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $quanid)->where('uid', $uid)->whereIn('zhuangtai', [$leixing])->get()->getRowArray();
		
	} elseif ($leixing==3) {
		$leixing='1,3';
		$guanliuid =\Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $quanid)->where('uid', $uid)->whereIn('zhuangtai', [$leixing])->get()->getRowArray();
		$yonghuzuuid =\Phpcmf\Service::M()->db->table('1_quan_form_group')->where('quanid', $quanid)->where('uid', $uid)->get()->getRowArray();
	    dr_debug('文件名称$uid',$uid.$quanid);
	} elseif ($leixing==4) {
		$leixing='1,3';
		$guanliuid =\Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $quanid)->where('uid', $uid)->whereIn('zhuangtai', [$leixing])->get()->getRowArray();
		$yonghuzuuid =\Phpcmf\Service::M()->db->table('1_quan_form_group')->where('quanid', $quanid)->where('uid', $uid)->get()->getRowArray();
	} else {
		$guanliuid = '';
	}
	if($yonghuzuuid){
		$yonghuzudata = $yonghuzuuid['gid'];
	} else {
		$yonghuzudata = $guanliuid['guanlizu'];
	}
	if($rt){
		$guanlimings =\Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('id', $yonghuzudata)->where('cid', $quanid)->where($mingcheng, 1)->get()->getRowArray();
	} else {
		$guanliuid = '';
	}
	
	
	if($guanlimings){
		$guanliming = $guanlimings;
	} else {
		$guanliming = '';
	}
	
    return $guanliming;
	
}
// 搜索关键词
function d_sousuo_data($value,$uid,$tongji) {
    $value;
    $ip = \Phpcmf\Service::L('input')->ip_address();
	//dr_debug('名称1', $value);
	//添加用户搜索数据
	$datas = [
	    //'site' => (int)$site,
	    'catid'=> '1', //类型
	    'title' => $value, //标题
	    'zhiding' => '1', //置顶
		'thumb' => '',
		'keywords' => '',
		'description'=>'',
	    'content'=> '', //内容
		'hits'=> '1',
	    'uid'=> (int)$uid, //
	    'author'=> '', //
	    'status'=> '',//
	    'url'=> '', //
	    'link_id'=> '', //
	    'tableid'=> '', //
	    'inputip'=> $ip, //
		'inputtime' => SYS_TIME,
		'updatetime' => SYS_TIME,
		'displayorder'=> '0',
	    'cishu'=> '1', //次数
	    'jieguoshu'=> '', //结果数 
	];
	$tianjia = \Phpcmf\Service::M()->table('1_sousuotongji')->where('catid', '1')->where('uid', $uid)->where('title', $value)->getRow();
	if($tianjia){
		\Phpcmf\Service::M()->db->table('1_sousuotongji')->where('catid', '1')->where('uid', $uid)->where('title', $value)->update([
		    'cishu' => (int)$tianjia['cishu'] +1,
		    'jieguoshu' => $tongji,
		]);
	} else {
		\Phpcmf\Service::M()->db->table('1_sousuotongji')->replace($datas);
		//dr_debug('文件$tr', $tr);
		//添加附表
		$fuiduid = \Phpcmf\Service::M()->db->insertID(); // id
		//dr_debug('$fuiduid', $fuiduid);
		//$datasousuo = \Phpcmf\Service::M()->db->table('1_sousuotongji')->where('catid','1')->orderBy('id DESC')->limit(1)->get()->getResultArray();
		$datac = [
			'id'=> $fuiduid,
		    'uid'=> (int)$uid, //发布人的uid
		    'catid'=> '1', //类型
		];
		\Phpcmf\Service::M()->db->table('1_sousuotongji_data_0')->replace($datac);
	}
	//添加搜索数据
    $datas = [
        //'site' => (int)$site,
        'catid'=> '2', //类型
        'title' => $value, //标题
        'zhiding' => '1', //置顶
		'thumb' => '',
		'keywords' => '',
		'description'=>'',
        'content'=> '', //内容
		'hits'=> '1',
        'uid'=> (int)$uid, //
        'author'=> '', //
        'status'=> '',//
        'url'=> '', //
        'link_id'=> '', //
        'tableid'=> '', //
        'inputip'=> $ip, //
		'inputtime' => SYS_TIME,
		'updatetime' => SYS_TIME,
		'displayorder'=> '0',
        'cishu'=> '1', //次数
        'jieguoshu'=> '', //结果数 
    ];
	$tianjia = \Phpcmf\Service::M()->table('1_sousuotongji')->where('catid', '2')->where('title', $value)->getRow();
	if($tianjia){
		\Phpcmf\Service::M()->db->table('1_sousuotongji')->where('catid', '2')->where('title', $value)->update([
		    'cishu' => (int)$tianjia['cishu'] +1,
			'uid' => (int)$uid,
			'jieguoshu' => $tongji,
		]);
	} else {
		\Phpcmf\Service::M()->db->table('1_sousuotongji')->replace($datas);
		//dr_debug('文件$tr', $tr);
		//添加附表
		$fuid = \Phpcmf\Service::M()->db->insertID();
		//$datasousuo = \Phpcmf\Service::M()->db->table('1_sousuotongji')->where('catid','2')->orderBy('id DESC')->limit(1)->get()->getResultArray();
		 // id
		//dr_debug('$fuid', $fuid);
		$datac = [
			'id'=> $fuid,
		    'uid'=> (int)$uid, //发布人的uid
		    'catid'=> '2', //类型
		];
		\Phpcmf\Service::M()->db->table('1_sousuotongji_data_0')->replace($datac);
	}
	$zongshu = \Phpcmf\Service::M()->table('1_sousuotongji')->where('catid', '2')->where('title', $value)->getRow();
    return $zongshu['cishu'];
}

// #话题# html代码pc端和h5端
function d_huati($data) {
	if(dr_is_app('tags')) {
		// 正则表达式匹配#之间的内容
		$pattern = '/#([^#;\s<>]+)#/'; // /\s*#([^#\s]+)#\s*/
		//$pattern = '/\b(#(?![ :#]).*?)(?=[ :#]|$)/u'; 
		// 使用preg_match_all提取所有匹配项  
		$datas = dr_clearhtml($data);
		preg_match_all($pattern, $datas, $matches);
		// $matches[1] 包含所有#之间的内容  
		$guanjiancidata = $matches[1];  
		
		$filteredData = array_filter($guanjiancidata, function($item) {  
            return strpos($item, ': #') === false && strpos($item, ':#') === false;  
        });
		
		// 使用preg_replace_callback替换匹配到的内容  
		$Textdata = preg_replace_callback($pattern, function($matches) use ($filteredData) {  
		    $key = $matches[1]; // 捕获#之间的内容  
		    //$link = isset($guanjiancidata[$key]) ? $guanjiancidata[$key] : ''; // 查找对应的链接  
			$key = dr_strcut($key, 10);
			$link = dr_url('tags/home/index',['k'=>$key]);
		    return "<a href='$link' class='d-jiacu d-zitiyanse-hongse'> #$key# </a>"; // 返回替换后的<a>标签  
		}, $data);
	} else {
		$Textdata = $data;
	}
	
    return $Textdata;
}
// #话题# html代码pc端和h5端
function d_huati_zu($data) {
	// 正则表达式匹配#之间的内容
	$pattern = '/#([^#;\s<>]+)#/';
	// 使用preg_match_all提取所有匹配项  
	$datas = dr_clearhtml($data);
	//dr_debug('文件名称$datas', $datas);
	preg_match_all($pattern, $datas, $matches);
	// $matches[1] 包含所有#之间的内容  
	$guanjiancidata = $matches[1];  
	//dr_debug('文件名称data', $guanjiancidata);
	// 过滤掉包含: # 或 :# 的项  
    $filteredData = array_filter($guanjiancidata, function($item) {  
        return $item !== ': #' && $item !== ':#';
    });  
	//dr_debug('文件名称$filteredData', $filteredData);
	$Textdata = [];
	$count = 0;
	// 循环  
	foreach ($filteredData as $i => $t) {  
		if ($i < 4) {  
		    $t = dr_strcut($t, 10);
		    $link = dr_url('tags/home/index',['k'=>$t]); // 如果找不到链接，使用#作为占位符  
		    $Textdata[] = "<a href='$link' style='display: -webkit-inline-box;' class='d-wenzi-12 d-neikuang-2-5 d-bian-1 d-yuanjiao-50 d-zitiyanse-hui d-you-5  d-shang-5'> <i class='fa fa-slack'></i> $t <i class='fa fa-slack'></i>  </a>";
		}
		
	}
	// 输出所有的<a>标签
	foreach ($Textdata as $link) {  
	    echo $link . ''; // 在每个链接后面添加一个空格，以便更好地显示  
	}
}
// #话题# 数据
function d_huati_zu_data($data) {
	// 正则表达式匹配#之间的内容
	$pattern = '/#([^#;\s<>]+)#/';
	$datas = dr_clearhtml($data);
	// 使用preg_match_all提取所有匹配项  
	preg_match_all($pattern, $datas, $matches);
	// $matches[1] 包含所有#之间的内容  
	$guanjiancidata = $matches[1];  
	//dr_debug('文件名称data', $guanjiancidata);
	$Textdata = [];
	// 循环  
	foreach ($guanjiancidata as $i => $t) { 
		if ($i < 4) {  
			$t = dr_strcut($t, 10);
			$link = dr_url('tags/home/index',['k'=>$t]); // 如果找不到链接，使用#作为占位符  
			$Textdata[] = "$t";
		}
	}
	// 输出所有的<a>标签
	foreach ($Textdata as $link) {  
	    echo $link . ','; // 在每个链接后面添加一个空格，以便更好地显示  
	}
}
// #话题# 数据
function d_huati_zu_data_mform($data) {
	// 正则表达式匹配#之间的内容
	$pattern = '/#([^#;\s<>]+)#/';
	$datas = dr_clearhtml($data);
	// 使用preg_match_all提取所有匹配项  
	preg_match_all($pattern, $datas, $matches);
	// $matches[1] 包含所有#之间的内容  
	$guanjiancidata = $matches[1];  
	//dr_debug('文件名称data', $guanjiancidata);
	$Textdata = [];
	// 循环  
	foreach ($guanjiancidata as $i => $t) { 
		if ($i < 4) { 
			$t = dr_strcut($t, 10);
			$link = dr_url('tags/home/index',['k'=>$t]); // 如果找不到链接，使用#作为占位符  
			$Textdata[] = "$t";
		}
	}
	// 输出所有的<a>标签
	$tag = [];
	foreach ($Textdata as $link) {  
	    $tag[] = $link; 
	}
	$output = implode(',', $tag);
	return $output;
}
// #话题# app端
function d_huati_app($data) {
    if(dr_is_app('tags')) {
		// 正则表达式匹配#之间的内容
		$pattern = '/#([^#;\s<>]+)#/';
		$datas = dr_clearhtml($data);
		// 使用preg_match_all提取所有匹配项  
		preg_match_all($pattern, $datas, $matches);
		// $matches[1] 包含所有#之间的内容  
		$guanjiancidata = $matches[1];
		// 使用preg_replace_callback替换匹配到的内容  
		$Textdata = preg_replace_callback($pattern, function($matches) use ($guanjiancidata) {  
		    $key = $matches[1]; // 捕获#之间的内容  
		    //$link = isset($guanjiancidata[$key]) ? $guanjiancidata[$key] : ''; // 查找对应的链接
			$key = dr_strcut($key, 10);
			$link = '/pages/tags/index?tags='.$key;
		    return "<a href='$link' id='d-huati_id' class='d-jiacu d-zitiyanse-hongse '> #$key# </a>"; // 返回替换后的<a>标签  
		}, $data);
	} else {
		$Textdata = $data;
	}
    return $Textdata;
}
// #话题# html代码pc端和h5端
function d_huati_zu_app($data) {
	// 正则表达式匹配#之间的内容
	$pattern = '/#([^#;\s<>]+)#/'; // /\s*#([^#\s]+)#\s*/
	$datas = dr_clearhtml($data);
	// 使用preg_match_all提取所有匹配项  
	preg_match_all($pattern, $datas, $matches);
	// $matches[1] 包含所有#之间的内容  
	$guanjiancidata = $matches[1];  
	//dr_debug('文件名称data', $guanjiancidata);
	$Textdata = [];
	$Textdata['flowItemValue']='';
	$Textdata['flowItemMargin']='5';
	$Textdata['flowItemRadius']='20';
	$Textdata['flowItemBgColor']='#eeeeee';
	$Textdata['flowItemTextColor']='#3a3a3a';
	// 循环  
	foreach ($guanjiancidata as $i => $t) { 
		if ($i < 4) { 
		$t = dr_strcut($t, 10);
	    $link = dr_url('pages/tags/index?tags=',['k'=>$t]); // 如果找不到链接，使用#作为占位符  
	    $Textdata['flowList'][$i] = $t;
		}
	    //$Textdata[$i]['id'] = $i;
	}
	
	// 输出所有的<a>标签
	foreach ($Textdata as $link) {  
	    //echo $link . ''; // 在每个链接后面添加一个空格，以便更好地显示  
	}
	return $Textdata;
}